package com.gkd.controller.admin;

import com.gkd.pojo.dto.CategoryDTO;
import com.gkd.pojo.dto.CategoryPageQueryDTO;
import com.gkd.pojo.entity.Category;
import com.gkd.pojo.vo.CategoryVO;
import com.gkd.result.PageResult;
import com.gkd.result.Result;
import com.gkd.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类相关接口
 */
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增类别
     * @param categoryDTO
     */
    @PostMapping
    public Result add(@RequestBody CategoryDTO categoryDTO){
        log.info("新增分类:{}",categoryDTO);
        categoryService.add(categoryDTO);
        return Result.success();
    }

    /**
     * 获取类别列表
     * @return
     */
    @GetMapping
    public Result<List<Category>> list(){
        log.info("获取类别列表");
        List<Category> list = categoryService.list();
        return Result.success(list);
    }

    /**
     * 类别分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("类别分页查询：{}",categoryPageQueryDTO);
        PageResult pageResult=categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id查找分类
     * @param categoryId
     * @return
     */
    @GetMapping("/{categoryId}")
    public Result<CategoryVO> getByid(@PathVariable Integer categoryId){
        log.info("根据id查找分类:{}",categoryId);

        CategoryVO categoryVO = new CategoryVO();
        Category category = categoryService.getById(categoryId);
        BeanUtils.copyProperties(category,categoryVO);

        return Result.success(categoryVO);
    }

    /**
     * 根据id修改分类
     * @param categoryDTO
     * @return
     */
    @PutMapping
    public Result update(@RequestBody CategoryDTO categoryDTO){
        log.info("根据id修改分类:{}",categoryDTO);
        categoryService.update(categoryDTO);
        return Result.success();
    }

    /**
     * 批量删除分类
     * @param categoryIds
     * @return
     */
    @DeleteMapping
    public Result deleteByIds(@RequestBody List<Integer> categoryIds){
        log.info("批量删除分类:{}",categoryIds);
        categoryService.deleteByIds(categoryIds);
        return Result.success();
    }


    /**
     * 更改分类状态
     * @param status
     * @param categoryId
     * @return
     */
    @PutMapping("/status/{status}")
    public Result status(@PathVariable Integer status,Integer categoryId){
        log.info("更改分类状态:id=={},status=={}",categoryId,status);
        Category category = Category.builder()
                .categoryId(categoryId)
                .status(status)
                .build();
        categoryService.updateById(category);
        return Result.success();
    }



}
